package World.Module.Fiber;

import Entity.SceneType;
import Task.JTask;
import World.ISingletonAwake;
import World.ISingletonReverseDispose;
import World.Singleton;

import java.util.concurrent.ConcurrentHashMap;

public class FiberManager extends Singleton implements ISingletonAwake, ISingletonReverseDispose {

    private final IScheduler[] schedulers = new IScheduler[3];

    private final int idGenerator = 10000000;// 10000000以下为保留的用于StartSceneConfig的fiber id, 1个区配置1000个纤程，可以配置10000个区

    private ConcurrentHashMap<Integer, Fiber> fibers = new ConcurrentHashMap<>();

    private MainThreadScheduler mainThreadScheduler;

    @Override
    public void Awake() {
        this.mainThreadScheduler = new MainThreadScheduler(this);
        this.schedulers[SchedulerType.Main.ordinal()] = this.mainThreadScheduler;

//        #if ENABLE_VIEW && UNITY_EDITOR
//        this.schedulers[(int)SchedulerType.Thread] = this.mainThreadScheduler;
//        this.schedulers[(int)SchedulerType.ThreadPool] = this.mainThreadScheduler;
//        #else
        this.schedulers[SchedulerType.Thread.ordinal()] = new MainThreadScheduler(this);
        this.schedulers[SchedulerType.ThreadPool.ordinal()] = new MainThreadScheduler(this);

    }

    public void Update()
    {
        this.mainThreadScheduler.Update();
    }

    public void LateUpdate()
    {
        this.mainThreadScheduler.LateUpdate();
    }

    @Override
    protected void Destroy()
    {
        for (IScheduler scheduler: this.schedulers) {
            scheduler.Dispose();
        }

        for(Fiber fiber : fibers.values()){
            fiber.Dispose();
        }

        this.fibers = null;
    }

    public JTask<Integer> Create(SchedulerType schedulerType, int fiberId, int zone, SceneType sceneType, String name)
    {

        Fiber fiber = new Fiber(fiberId, zone, sceneType, name);


        return null;


    }

}
